<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="1"
        :formType="type"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryBrandDictOwnerOEM, addSeriesOEM,putSeriesOEM } from '@/api/common/cars/brandcarsModels';

import Cookies from 'js-cookie';
export default {
  name: 'SeriesModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
    };
  },
  created() {
    this.init();
    this.getDictData();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        this.formList.find(item=>item.fieldName=="brandId").disabled=true
        this.formList.find(item=>item.fieldName=="seriesCode").disabled=true
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌名称',
          fieldName: 'brandId',
          placeholder: '请选择',
          itemList: [],
          disabled:false,
          rules: [{ required: true, message: '请选择品牌名称', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '车系代码',
          disabled:false,
          fieldName: 'seriesCode',
          placeholder: '请输入...',
          maxlength:64,
          rules: [ 
            { required: true, message: '请输入车系代码', trigger: 'blur' },
            // { pattern: /^[\d|a-zA-Z|-]{1,}$/, message: '请输入字母或数字', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '请输入...',
          maxlength:64,
          rules: [{ required: true, message: '请输入车系名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择有效性', trigger: 'change' }]
        },

      ];
    },
    async getDictData() {
      const res = await queryBrandDictOwnerOEM();
      if (res.resultCode === 200) {
        this.formList.find(item=>item.fieldName==='brandId').itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.recordVersion = this.rowData.recordVersion
        if(this.type == 'add'){
          const res = await addSeriesOEM(this.saveData);
          if (res.resultCode === 200) {
            this.closeDialog('refresh');
          }
        }else{
          const res = await putSeriesOEM(this.saveData,this.rowData.seriesId);
          if (res.resultCode === 200) {
            this.closeDialog('refresh');
          }
        }

      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
